package com.tedu.ivos.dict.service.impl;

import com.tedu.ivos.dict.mapper.DictMapper;
import com.tedu.ivos.dict.pojo.dto.DictQuery;
import com.tedu.ivos.dict.pojo.dto.DictSaveParam;
import com.tedu.ivos.dict.pojo.entity.Dict;
import com.tedu.ivos.dict.pojo.vo.DictVO;
import com.tedu.ivos.dict.service.IDictService;
import com.tedu.ivos.dictoption.mapper.DictOptionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class DictServiceImpl implements IDictService {
    @Autowired
    DictMapper dictMapper;
    @Autowired
    DictOptionMapper dictOptionMapper;

    @Override
    public List<DictVO> select(DictQuery dictQuery) {
        log.debug("查询字典业务，参数：{}",dictQuery);
        return dictMapper.select(dictQuery);
    }

    @Override
    public void save(DictSaveParam dictSaveParam) {
        log.debug("保存字典业务，参数：{}",dictSaveParam);
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictSaveParam,dict);
        if(dict.getId()!=null){
            log.debug("更新字典业务，参数：{}",dict);
            //TODO 编辑字典
            dict.setUpdateTime(new Date());
            dictMapper.update(dict);
        }else {
            log.debug("新增字典业务，参数：{}",dict);
            dict.setCreateTime(new Date());
            dictMapper.insert(dict);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("删除字典业务，参数：{}",id);
        dictMapper.delete(id);
        //删除字典，同时也要删除该字典下的所有字典项 否则会成死数据占用空间！
        dictOptionMapper.deleteByDictId(id);
    }
}
